"""
Name:abnerclc
Email:abnerclc@hotmail.com
Date:2024.10.12
Env:Python3.6
并发模型
"""
import sys
from multiprocessing import Process
from socket import *
HOST = "0.0.0.0"
PORT = 8888
ADDR = (HOST,PORT)
def handle(conn,address=("",0)):
    while True:
        data = conn.recv(1024)
        if not data:
            break
        print(data.decode())
        conn.send(b"Test")
    conn.close()
def main():
    sock = socket()
    sock.bind(ADDR)
    sock.listen(5)
    print("listen the port:%d"%PORT)
    while True:
        try:
            conn,addr = sock.accept()
            print("connect from",addr)
        except KeyboardInterrupt:
            sock.close()
            sys.exit("服务结束")
        p = Process(target=handle,kwargs={"conn":conn,"address":addr})
        p.start()
if __name__ == '__main__':
    main()